package hai.com.android_test.widget;

import android.content.Context;
import android.support.annotation.LayoutRes;
import android.support.v4.view.ViewPager;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;

import java.util.ArrayList;
import java.util.List;

import hai.com.android_test.R;
import hai.com.android_test.adapter.MyPageAdapter;

/**
 * viewpage轮播图
 * Created by 黄海 on 2017/1/9.
 */
public class AutoViewPageWtihIndicator extends LinearLayout {

    Context context;
    ViewPager viewPage;
    LinearLayout indicatorContainer;
    private Integer[] imageIds;
    List<View> views = new ArrayList();

    @LayoutRes
    private int indicatorView;//have the attribute selected true and false

    public AutoViewPageWtihIndicator(Context context) {
        this(context, null);
    }

    public AutoViewPageWtihIndicator(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }
    private void init() {
        context = getContext();
        View view = LayoutInflater.from(getContext()).inflate(R.layout.auto_viewpage_with_indicator, this,true);
        viewPage = (ViewPager) view.findViewById(R.id.viewPage);
        indicatorContainer = (LinearLayout) view.findViewById(R.id.indicatorContainer);
    }

    public void setImageIds(final Integer[] ids, @LayoutRes int indicator, boolean autoScroll) {
        views.clear();
        this.imageIds = ids;
        for (Integer id : ids) {
            ImageView view = new ImageView(context);
            view.setScaleType(ImageView.ScaleType.FIT_XY);
            view.setImageResource(id);
            views.add(view);
        }
        viewPage.setAdapter(new MyPageAdapter(views));
        if (ids != null) {
            for (int i = 0; i < ids.length; i++) {
                View indi = LayoutInflater.from(context).inflate(indicator, null);
                indi.setSelected(false);
                indicatorContainer.addView(indi);
            }
        }
    }

}
